﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using RoyTools.D3Monitor.FormUI.Properties;

namespace RoyTools.D3Monitor.FormUI.Helper
{
    /// <summary>
    /// 邮件辅助类
    /// </summary>
    public class MailHelper
    {
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="body"></param>
        public static void Send(string subject, string body)
        {
            try
            {
                var from = new MailAddress(Program.Config.MailSetting.From);
                var to = new MailAddress(Program.Config.MailSetting.To);
                using (var mailMessage = new MailMessage())
                {
                    mailMessage.Subject = subject;
                    mailMessage.From = from;
                    mailMessage.To.Add(to);

                    mailMessage.Body = body;
                    mailMessage.BodyEncoding = Encoding.UTF8;
                    mailMessage.IsBodyHtml = false;
                    mailMessage.Priority = MailPriority.Normal;

                    using (var smtpClient = new SmtpClient(Program.Config.MailSetting.Smtp, Program.Config.MailSetting.Port))
                    {
                        smtpClient.Credentials = new NetworkCredential(Program.Config.MailSetting.Account, Program.Config.MailSetting.Password);
                        smtpClient.EnableSsl = true;
                        smtpClient.Send(mailMessage);
                        smtpClient.Dispose();
                    }
                    mailMessage.Dispose();
                }
            }
            catch 
            {
                Program.Default.tslMessage.Text = Resources.SendMailFailed;
            }
        }
    }
}
